//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.lengmomo.games.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lengmomo.games.pojo.enums.HttpCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 抽象公共controller
 * </p>
 * 用于返回页面数据
 *  SuccessModelMap 用于返回永远200成功
 *  FailModelMap  用于返回500失败
 * @author fulin
 * @company 润兴科技
 * @since 2018-10-17
 */
//@Slf4j
@SuppressWarnings({"all"})
public abstract class AbstractController {


    public AbstractController() {
    }

    /********************************200**********************************/
    protected ResponseEntity<ModelMap> SuccessModelMap() {
        return this.SuccessModelMap(new ModelMap(), (Object) null);
    }

    protected ResponseEntity<ModelMap> SuccessModelMap(ModelMap modelMap) {
        return this.SuccessModelMap(modelMap, (Object) null);
    }

    protected ResponseEntity<ModelMap> SuccessModelMap(Object data) {
        return this.setModelMap(new ModelMap(), HttpCode.OK, data);
    }

    protected ResponseEntity<ModelMap> SuccessModelMap(ModelMap modelMap, Object data) {
        return this.setModelMap(modelMap, HttpCode.OK, data);
    }

    protected ResponseEntity<ModelMap> SuccessModelMap(String message) {
        return this.setModelMap(HttpCode.OK, message);

    }

    protected ResponseEntity<ModelMap> SuccessModelMap(String message, Object data) {
        return this.setModelMap(HttpCode.OK, message, data);

    }

    /********************************500**********************************/
    protected ResponseEntity<ModelMap> FailModelMap() {
        return this.FailModelMap(new ModelMap(), (Object) null);
    }

    protected ResponseEntity<ModelMap> FailModelMap(ModelMap modelMap) {
        return this.FailModelMap(modelMap, (Object) null);
    }

    protected ResponseEntity<ModelMap> FailModelMap(Object data) {
        return this.setModelMap(new ModelMap(), HttpCode.INTERNAL_SERVER_ERROR, data);
    }

    protected ResponseEntity<ModelMap> FailModelMap(ModelMap modelMap, Object data) {
        return this.setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR, data);
    }

    protected ResponseEntity<ModelMap> FailModelMap(String message) {
        return this.setModelMap(HttpCode.INTERNAL_SERVER_ERROR, message);
    }

    protected ResponseEntity<ModelMap> FailModelMap(String message, Object data) {
        return this.setModelMap(HttpCode.INTERNAL_SERVER_ERROR, message, data);
    }

    /********************************自定义**********************************/

    protected ResponseEntity<ModelMap> setModelMap(HttpCode code) {
        return this.setModelMap((ModelMap) (new ModelMap()), (HttpCode) code, (Object) null);
    }

    protected ResponseEntity<ModelMap> setModelMap(String code, String message) {
        return this.setModelMap(new ModelMap(), code, message, (Object) null);
    }

    protected ResponseEntity<ModelMap> setModelMap(HttpCode code, String message) {
        return this.setModelMap(new ModelMap(), code.code().toString(), message, (Object) null);
    }

    protected ResponseEntity<ModelMap> setModelMap(HttpCode code, String message, Object data) {
        return this.setModelMap(new ModelMap(), code.code().toString(), message, data);
    }

    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code) {
        return this.setModelMap((ModelMap) modelMap, (HttpCode) code, (Object) null);
    }

    protected ResponseEntity<ModelMap> setModelMap(HttpCode code, Object data) {
        return this.setModelMap(new ModelMap(), code, data);
    }

    protected ResponseEntity<ModelMap> setModelMap(String code, String message, Object data) {
        return this.setModelMap(new ModelMap(), code, message, data);
    }


    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code, Object data) {
        return this.setModelMap(modelMap, code.code().toString(), code.reasonPhraseCN(), data);
    }

    protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, String code, String message, Object data) {
        if (!modelMap.isEmpty()) {
            Map<String, Object> map = new LinkedHashMap<>(modelMap);
            modelMap.clear();
            for (String key : map.keySet()) {
                if (!key.startsWith("org.springframework.validation.BindingResult") && !key.equals("void")) {
                    modelMap.put(key, map.get(key));
                }
            }
        }
        if (StringUtils.isNotBlank(message)) {
            modelMap.put("message", message);
        } else {

        }
        if (data != null) {
            if (data instanceof IPage) {
                IPage<?> page = (IPage) data;
                modelMap.put("rows", page.getRecords());
                modelMap.put("size", page.getSize());
                modelMap.put("pages", page.getPages());
                modelMap.put("total", page.getTotal());
            } else if (data instanceof List) {
                modelMap.put("rows", data);
                modelMap.put("total", ((List) data).size());
            } else {
                modelMap.put("data", data);
            }
        }
        modelMap.put("code", code);
        modelMap.put("timestamp", System.currentTimeMillis());
//        log.info("response===>{}", JSON.toJSONString(modelMap));
        return ResponseEntity.ok(modelMap);
    }
    /**
     * 重定向至地址 url
     *
     * @param url 请求地址
     * @return
     */
    protected String redirectTo(String url) {
        return "redirect:" + url;
    }
}
